<template>
  <div class="container">
    <div id="formHeader">
      <!-- 新增按钮 -->
      <el-form style="margin-right: 20px">
        <el-form-item>
          <el-button
            type="primary"
            @click="dialogAddVisible = true"
            icon="el-icon-circle-plus-outline"
          >
            新增
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 新增表单 -->
      <template>
        <el-dialog
          title="新增平台管理员"
          :visible.sync="dialogAddVisible"
          @close="addAdminHandel"
        >
          <el-form :model="form">
            <el-form-item label="账号" :label-width="formLabelWidth">
              <el-input
                v-model="form.account"
                autocomplete="off"
                placeholder="输入账号..."
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" :label-width="formLabelWidth">
              <el-input
                v-model="form.password"
                autocomplete="off"
                placeholder="输入密码..."
              ></el-input>
            </el-form-item>
            <el-form-item label="昵称" :label-width="formLabelWidth">
              <el-input
                v-model="form.name"
                autocomplete="off"
                placeholder="输入昵称..."
              ></el-input>
            </el-form-item>
            <el-form-item label="年龄" :label-width="formLabelWidth">
              <el-input
                v-model="form.age"
                autocomplete="off"
                placeholder="输入年龄..."
              ></el-input>
            </el-form-item>
            <el-form-item label="性别" :label-width="formLabelWidth">
              <el-radio v-model="form.gender" label="男">男</el-radio>
              <el-radio v-model="form.gender" label="女">女</el-radio>
            </el-form-item>
            <el-form-item label="电话" :label-width="formLabelWidth">
              <el-input
                v-model="form.phone"
                autocomplete="off"
                placeholder="输入电话..."
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogAddVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogAddVisible = false"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </template>
      <!-- 分类查询 -->
      <el-form>
        <el-form-item>
          <el-select v-model="alllist" style="width: 120px" placeholder="所有">
            <el-option
              v-for="(item, index) in typeList"
              :key="index"
              :value="item.type"
              :label="item.title"
            ></el-option>
          </el-select>
          <!-- 搜索框 -->
          <el-input
            v-model="inputValue"
            placeholder="请输入内容"
            class="this-input"
            style="margin-left: 5px; width: 200px"
          ></el-input>
          <!-- 搜索按钮 -->
          <el-button
            @click="upClick"
            type="primary"
            icon="el-icon-search"
            style="margin-left: 15px"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 修改弹出框 -->
    <template>
      <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="昵称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" :label-width="formLabelWidth">
            <el-input v-model="form.account" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-radio v-model="form.gender" label="男">男</el-radio>
            <el-radio v-model="form.gender" label="女">女</el-radio>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updataOkClick"
            ><div>确 定</div></el-button
          >
        </div>
      </el-dialog>
    </template>
    <!-- 表格 -->
    <div>
      <template>
        <el-table
          :data="
            tableData.slice(
              (currentPage - 1) * pageSize,
              currentPage * pageSize
            )
          "
          border
          style="width: 100%"
        >
          <el-table-column prop="name" label="昵称"> </el-table-column>
          <el-table-column prop="account" label="账号"> </el-table-column>
          <el-table-column prop="age" label="年龄"> </el-table-column>
          <el-table-column prop="gender" label="性别"> </el-table-column>
          <el-table-column prop="phone" label="电话"> </el-table-column>
          <el-table-column prop="position" label="职位">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.position == 0">平台管理员</el-tag>
              <el-tag v-if="scope.row.position == 1">门店管理员</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == 0">申请中</el-tag>
              <el-tag v-if="scope.row.status == 1">已通过</el-tag>
              <el-tag v-if="scope.row.status == 2">未通过</el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="140">
            <template #default="scope">
              <el-row class="butto">
                <div @click="updataClick(scope.row)">
                  <el-button
                    type="primary"
                    size="small"
                    @click="dialogFormVisible = true"
                    >修改</el-button
                  >
                </div>
                <div>
                  <el-button
                    type="danger"
                    size="small"
                    @click="deClick(scope.row)"
                    >删除</el-button
                  >
                </div>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import service from "../../utils/request";
export default {
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 5, // 每页条数
      total: "", // 总条数
      tableData: [],
      inputValue: "",
      alllist: "all",
      typeList: [
        { type: "all", title: "所有" },
        { type: "position", title: "职位" },
        { type: "status", title: "状态" },
        { type: "age", title: "年龄" },
        { type: "name", title: "昵称" },
        { type: "gender", title: "性别" },
        { type: "phone", title: "电话" },
      ],
      form: {
        account: "",
        name: "",
        age: "",
        gender: "",
        phone: "",
        position: "",
        status: "",
      },
      dialogFormVisible: false,
      dialogAddVisible: false, // 新增
      formLabelWidth: "120px",
    };
  },

  methods: {
    // 分页功能
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      this.currentPage = val;
    },

    // 查询功能
    upClick() {
      if (this.alllist == "all") {
        service.get(`/admin`).then((res) => {
          this.tableData = res.rows;
        });
      } else {
        service
          .get(`/admin?value=${this.inputValue}&type=${this.alllist}`)
          .then((res) => {
            this.tableData = res.rows;
            this.total = res.total;
            this.inputValue = ""; // 清空表单
          });
      }
    },
    // 修改
    updataClick(scope) {
      this.$store.dispatch("admin/getidadminInfo", scope._id).then((res) => {
        this.form = res;
      });
    },
    updataOkClick() {
      this.$store.dispatch("admin/putadminInfo", this.form).then(() => {
        this.$store.dispatch("admin/getadminsInfo").then((res) => {
          this.tableData = res.rows;
        });
      });
      this.dialogFormVisible = false;
    },
    // 删除(应该使用混入技术)
    deClick(scope) {
      this.$store.dispatch("admin/deleteadminInfo", scope._id).then(() => {
        this.$store.dispatch("admin/getadminsInfo").then((res) => {
          this.tableData = res.rows;
        });
      });
    },
    // 新增管理员
    addAdminHandel() {
      // 电话号码验证
      let reg =
        /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])d{8}$/;
      // if (reg.test(this.form.phone)) {
      this.$store.dispatch("admin/postadminInfo", this.form).then(() => {
        // 重新渲染数据
        this.$store.dispatch("admin/getadminsInfo").then((res) => {
          this.tableData = res.rows;
        });
      });
      // } else {
      //   alert("请输入正确的手机格式");
      // }
    },
  },
  mounted() {
    // 渲染数据
    this.$store.dispatch("admin/getadminsInfo").then((res) => {
      // 筛选出门店管理员
      let excludePlatform = res.rows.filter((item) => item.position == 1);
      this.tableData = excludePlatform;
    });
  },
};
</script>

<style scoped>
.container {
  padding: 20px;
}
.this-input {
  width: 200px;
  height: 30px;
}
#formHeader {
  display: flex;
}
.butto {
  display: flex;
  justify-content: space-around;
}
/* 分页 */
.container .el-pagination {
  margin: 20px auto;
}
</style>